﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WindowsFormsApp1.Docking;
using WindowsFormsApp1.Theme;

namespace WindowsFormsApp1
{
    public partial class MainForm : BaseForm
    {
        private readonly DockContent _toolBoxWindow = new ToolBoxWindow();
        private readonly PropertiesWindow _propertiesWindow = new PropertiesWindow();
        private readonly SolutionExplorer _solutionExplorer = new SolutionExplorer();
        private readonly DocumentWindow _documentWindow = new DocumentWindow();

        private ToolStrip toolbar;

        public MainForm()
        {
            FormBorderStyle = FormBorderStyle.None;
            //AutoScaleMode = AutoScaleMode.Dpi;

            InitializeComponent();
            InitializeDockePanal();
            CreateCustomTitleBar();
            //SetupMenu();
        }
        public void InitializeDockePanal()
        {
            var theme = new CustomDarkTheme();
            dockPanel.Theme = theme;

            // 主文档窗口
            _documentWindow.Show(dockPanel, DockState.Document);

            // 左侧窗口
            _toolBoxWindow.Show(dockPanel, DockState.DockLeft);
            _solutionExplorer.Show(dockPanel, DockState.DockLeft);
            //_solutionExplorer.DockTo(_toolBoxWindow.Pane, DockAlignment.Bottom, 0.4);

            // 右侧属性窗口
            _propertiesWindow.Show(dockPanel, DockState.DockRight);

        }

        // 消除工具栏灰色边框
        private void toolbar_Paint(object sender, PaintEventArgs e)
        {
            if ((sender as ToolStrip).RenderMode == ToolStripRenderMode.System)
            {
                Rectangle rect = new Rectangle(0, 0, this.toolbar.Width, this.toolbar.Height - 2);
                e.Graphics.SetClip(rect);
            }
        }

        private void SetupMenu()
        {
            var panel = new Panel();

            // 工具栏
            toolbar = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow,
                Name = "toolbar",
                AllowDrop = true,
                AutoSize = true,
                MinimumSize = new Size(0, 32),
                ImageScalingSize = new Size(24,24),
                BackColor = Color.FromArgb(45, 45, 48),
                ForeColor = Color.FromArgb(189, 189, 189),
                GripStyle = ToolStripGripStyle.Hidden,
                RenderMode = ToolStripRenderMode.System
            };

            toolbar.Paint += new PaintEventHandler(toolbar_Paint);
          
            toolbar.Items.Add(new ToolStripButton
            {
                Image = Properties.Resources.livephoto,
                ToolTipText = "新建"
            });
            toolbar.Items.Add(new ToolStripButton
            {
                Image = Properties.Resources.livephoto,
                ToolTipText = "打开"
            });
            toolbar.Items.Add(new ToolStripButton
            {
                Image = Properties.Resources.livephoto,
                ToolTipText = "保存"
            });
            Controls.Add(toolbar);

            // 菜单栏
            var menuStrip = new MenuStrip 
            { 
                MinimumSize = new Size(0,32),
                ForeColor = Color.FromArgb(189, 189, 189),
                BackColor = Color.FromArgb(45, 45, 48) 
            };
            var fileMenu = new ToolStripMenuItem("文件(&F)");
            fileMenu.DropDownItems.AddRange(new ToolStripItem[] {
                new ToolStripMenuItem("新建(&N)"),
                new ToolStripMenuItem("打开(&O)..."),
                new ToolStripSeparator(),
                new ToolStripMenuItem("退出(&X)")
            });
            menuStrip.Items.Add(fileMenu);
            Controls.Add(menuStrip);

            menuStrip.PerformLayout();
            toolbar.PerformLayout();
        }

        private void CreateCustomTitleBar()
        {
            // 容器面板（自定义标题栏）
            var titleBarPanel = new Panel
            {
                Height = 54,
                Dock = DockStyle.Top,
                BackColor = Color.FromArgb(255, 45, 45, 48),
                Padding = new Padding(0, 0, 0, 0)
            };

            
            // 左侧Logo区域
            var logoPanel = new Panel
            {
                Width = 50,
                Dock = DockStyle.Left,
                Padding = new Padding(10, 0, 0, 0),
                BackColor = Color.Transparent
            };

            var logoPic = new PictureBox
            {
                Image = Properties.Resources.livephoto, // 从资源加载
                SizeMode = PictureBoxSizeMode.Zoom,
                Dock = DockStyle.Left,
                Width = 30,
                Height = 30
            };
            logoPanel.Controls.Add(logoPic);

            // 右侧控制按钮容器
            var controlPanel = new FlowLayoutPanel
            {
                //Width = 120,
                Dock = DockStyle.Right,
                FlowDirection = FlowDirection.RightToLeft,
                BackColor = Color.Transparent
            };

            // 创建控制按钮
            var controls = new[] { "✕", "☐", "—" };
            foreach (var c in controls)
            {
                var btn = new Button
                {
                    Text = c,
                    Size = new Size(60, 54),
                    FlatStyle = FlatStyle.Flat,
                    Font = new Font("Microsoft Sans Serif", 12),
                    ForeColor = Color.WhiteSmoke,
                    Margin = new Padding(0),
                    BackColor = Color.Transparent,
                    Tag = c // 用Tag存储按钮类型
                };

                if (c == "✕") btn.ForeColor = Color.LightCoral;

                btn.FlatAppearance.BorderSize = 0;
                btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 54, 65);
                btn.FlatAppearance.MouseDownBackColor = Color.FromArgb(60, 65, 80);

                btn.Click += ControlButton_Click;
                controlPanel.Controls.Add(btn);
            }
            // 菜单栏
            var menuStrip = new MenuStrip
            {
                Dock = DockStyle.Fill,
                AutoSize = true,
                Size = new Size(0,12),
                //RenderMode = ToolStripRenderMode.Professional,
                Renderer = new DarkMenuRenderer(),
                BackColor = Color.FromArgb(255, 45, 45, 48), //Color.FromArgb(30, 33, 40), //Color.WhiteSmoke, 
                //ForeColor = Color.Red,
            };
            // 添加窗体拖动功能
            menuStrip.MouseDown += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    NativeMethods.ReleaseCapture();
                    NativeMethods.SendMessage(this.Handle, 0xA1, 0x2, 0);
                }
            };

            // 菜单项
            // 文件菜单
            var fileMenu = new ToolStripMenuItem("文件(&F)");
            fileMenu.DropDownItems.AddRange(new ToolStripItem[] {
                new ToolStripMenuItem("新建(&N)", null, null, "Ctrl+N"),
                new ToolStripMenuItem("打开(&O)...", null, null, "Ctrl+O"),
                new ToolStripSeparator(),
                new ToolStripMenuItem("保存(&S)", null, null, "Ctrl+S"),
                new ToolStripMenuItem("另存为(&A)..."),
                new ToolStripSeparator(),
                new ToolStripMenuItem("退出(&X)")
            });

            // 编辑菜单
            var editMenu = new ToolStripMenuItem("编辑(&E)");
            editMenu.DropDownItems.AddRange(new ToolStripItem[] {
                new ToolStripMenuItem("撤销(&U)", null, null, "Ctrl+Z"),
                new ToolStripMenuItem("重做(&R)", null, null, "Ctrl+Y"),
                new ToolStripSeparator(),
                new ToolStripMenuItem("剪切(&T)", null, null, "Ctrl+X"),
                new ToolStripMenuItem("复制(&C)", null, null, "Ctrl+C"),
                new ToolStripMenuItem("粘贴(&P)...", null, null, "Ctrl+V")
            });

            // 视图菜单
            var viewMenu = new ToolStripMenuItem("视图(&V)");
            viewMenu.DropDownItems.AddRange(new ToolStripItem[] {
                new ToolStripMenuItem("工具栏"),
                new ToolStripMenuItem("状态栏"),
                new ToolStripMenuItem("全屏模式(&F)", null, null, "F11")
            });

            menuStrip.Items.AddRange(new ToolStripItem[] {
                fileMenu,
                editMenu,
                viewMenu,
                new ToolStripMenuItem("帮助(&H)")
            });

            // 添加所有控件到标题栏容器
            titleBarPanel.Controls.Add(menuStrip);
            titleBarPanel.Controls.Add(controlPanel);
            titleBarPanel.Controls.Add(logoPanel);

            // 添加到窗体
            this.Controls.Add(titleBarPanel);
        }

        private void ControlButton_Click(object sender, EventArgs e)
        {
            if (sender is Button btn)
            {
                switch (btn.Tag as string)
                {
                    case "—":
                        this.WindowState = FormWindowState.Minimized;
                        break;
                    case "☐":
                        if (this.WindowState == FormWindowState.Maximized)
                        {
                            this.WindowState = FormWindowState.Normal;
                            btn.Text = "☐";
                        }
                        else
                        {
                            this.WindowState = FormWindowState.Maximized;
                            btn.Text = "❏";
                        }
                        break;
                    case "✕":
                        this.Close();
                        break;
                }
            }
        }
    }
    // 使用Windows API实现窗体拖动
    internal static class NativeMethods
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
    }


}
